/**
 * 功能描述 列表
 */
define(function (require, exports, module) {
	var Grid = require("grid"); 	
	var aw = require("ajaxwrapper")
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");	
	
	var destinationfile = require("../store/destinationfile");
	
    var instance = {
        initComponent: function (params, widget) {
        	var grid = new Grid({
                parentNode : ".J-grid",
                url : "api/destination/query",
                params : function(){
                		var obj={
    	     					fetchProperties:destinationfile.fetchProperties
                		}
                		return obj;
                },
                model : {
                    columns : [{
                        name : "name",
                        label : "目的地"
                    },{
                        name : "address.fullName",
                        label : "地址",
                        format : function (value, row){
                        	return row.address.fullName + ' ' + row.street;
                        }
                    },{
                        name : "contact",
                        label : "联系人"
                    },{
                        name : "phone",
                        label : "联系电话"
                    },{
                        name : "status.value",
                        label : "状态"
                    },{
                        name : "show",
                        label : "操作",
    					format:"button",
						formatparams:[{
							id:"open",
							text:"启用",
							show:function(value,row){
								if(!row.pkDestination){
									return false;
								}
								if(row.status.key == "Setting"){
									return false;
								}else{
									return true;
								}
							},
							handler:function(index,data,rowEle){
								aw.ajax({
									url:"api/destination/save",
									data:{
										pkDestination:data.pkDestination,
										status:"Setting",
										version:data.version,
									},
									dataType:"json",
									success:function(data){
										widget.get("grid").refresh();
									}
								});
							 }
 						},{
							id:"close",
							text:"停用",
							show:function(value,row){
								if(!row.pkDestination){
									return false;
								}
								if(row.status.key == "BlockUp"){
									return false;
								}else{
									return true;
								}
							},							
							handler:function(index,data,rowEle){
							aw.ajax({
								url:"api/destination/save",
								data:{
									pkDestination:data.pkDestination,
									status:"BlockUp",
									version:data.version,
								},
								dataType:"json",
								success:function(data){
									widget.get("grid").refresh();
								}
							});
						 }
						},{
							id:"edit",
							icon:"icon-edit",
							handler:function(index,data,rowEle){
								widget.get("form").reset();
								widget.hide([".J-grid"]).show([".J-form"]);
								widget.get("subnav").hide(["add","search"]).show(["return"]);
								widget.get("form").setData(data);
								widget.get("form").setValue("status",data.status.key);
								widget.get("form").setValue("organization",data.organization.pkOrganization);
								widget.get("form").download("photo","api/attachment/destination/destinationphoto"+"_"+data.pkDestination);
							}
						},{
							id:"delete",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								aw.del("api/destination/"+data.pkDestination+"/delete",function() {
									grid.refresh();
								});	
							}
						}]
                    }]
                }
            });
        	widget.set("grid",grid);
        }
    };
    module.exports = instance;
});
